/***************************************************************************
***************************************************************************
***************************************************************************
***************************************************************************
The code in this file replicates Table 2. The code uses the sample "pseudo_chinaimp_comp" and the Stata function "ivreghdfe." If you do not have this function installed, refer to the following website for more details: https://scorreia.com/software/reghdfe/install.html

For more information on the variables and the sample, refer to the  "Read Me-China Import & Competition" file.
***************************************************************************
***************************************************************************
***************************************************************************
***************************************************************************/	

use pseudo_chinaimp_comp,replace
***replicates column 1 of Table 2, Panel A
				
	ivreghdfe xrdw_atw cr4     (liu liusq=lio liosq) ,abs(sic4  year segnum) vce(cluster gvkey)
	outreg2 using ivcomp_inv.xls, append dec(3) tstat ctitle(allxrd) keep(liu liusq ) 

***replicates column 2 of Table 2, Panel A

	ivreghdfe patnumc cr4     (liu liusq=lio liosq) ,abs(sic4  year segnum) vce(cluster gvkey)
	outreg2 using ivcomp_inv.xls, append dec(3) tstat ctitle(allpat) keep(liu liusq ) 
	
***replicates columns 3-6 of Panel A, all columns of Panels B and C:
	foreach j in 2 3 4{
		
		foreach v in 1  `j'{
			
			foreach m in xrdw_atw patnumc{
				
				ivreghdfe `m' cr4 (liu liusq=lio liosq) if d2vscopeq`j'==`v',abs(sic4 year segnum) vce(cluster gvkey)
				outreg2 using ivcomp_inv.xls, append dec(3) tstat ctitle(q`j'_c`v'_`m') keep(liu liusq ) 

			}
			
		}
		
	}